<template>
  <div class="content">
    <div class="header">
      <h2 class="title">用户列表</h2>
      <el-button type="primary" @click="handleCreateUserBtnClick">新建用户</el-button>
    </div>
    <div class="table">
      <el-table :data="userList" border style="width: 100%">
        <el-table-column type="selection" width="60px" align="center" />
        <el-table-column type="index" label="序号" width="60px" align="center" />
        <el-table-column label="用户名" prop="name" align="center" />
        <el-table-column label="真实姓名" prop="realname" align="center" />
        <el-table-column label="手机号码" prop="cellphone" align="center" />
        <el-table-column label="状态" prop="enable" align="center">
          <template #default="scope">
            <el-tag v-if="scope.row.enable === 1" type="success">启用</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createAt" align="center">
          <template #default="scope">
            {{ formatUTC(scope.row.createAt) }}
          </template>
        </el-table-column>
        <el-table-column label="更新时间" prop="updateAt" align="center">
          <template #default="scope">
            {{ formatUTC(scope.row.updateAt) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="150px" align="center">
          <template #default="scope">
            <el-button
              link
              size="small"
              type="primary"
              icon="Edit"
              @click="handleEditBtnClick(scope.row)"
              >编辑</el-button
            >
            <el-button
              link
              size="small"
              type="danger"
              icon="Delete"
              @click="handleDeleteBtnClick(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 30, 40]"
        layout="jumper, sizes, prev, pager, next, total"
        :total="userTotalCount"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import useSystemStore from '@/store/main/system/system'
import { storeToRefs } from 'pinia'
import { formatUTC } from '@/utils/format'
import { ref } from 'vue'

const emit = defineEmits(['createBtnClick', 'editBtnClick', 'ok'])

const systemStore = useSystemStore()

// 获取 userList 数据进行展示
const { userList, userTotalCount } = storeToRefs(systemStore)
console.log(userList)

// 页码相关逻辑
const currentPage = ref(1)
const pageSize = ref(10)
const handleSizeChange = () => {
  fetchUserListData()
}

const handleCurrentChange = () => {
  fetchUserListData()
}

// 分页请求函数
function fetchUserListData(formData: any = {}) {
  const size = pageSize.value
  const offset = (currentPage.value - 1) * size

  const queryInfo = { size, offset, ...formData }

  systemStore.postUserListAction(queryInfo)
}

fetchUserListData()

defineExpose({ fetchUserListData })

// 删除数据
function handleDeleteBtnClick(id: number) {
  systemStore.deleteUserByIdAction(id).then(() => {
    emit('ok')
  })
}

// 编辑数据
function handleEditBtnClick(itemData: any) {
  emit('editBtnClick', itemData)
}

// 新建数据
function handleCreateUserBtnClick() {
  emit('createBtnClick')
}
</script>

<style lang="less" scoped>
.content {
  margin-top: 20px;
  padding: 20px;
  background: #fff;

  .header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    margin-bottom: 10px;

    .title {
      font-size: 22px;
    }
  }
}

.table {
  :deep(.el-table__cell) {
    padding: 12px 0;
  }

  .el-button {
    margin-left: 0;
    padding: 5px 8px;
  }
}

.pagination {
  display: flex;
  justify-content: flex-end;
  margin-top: 10px;
}
</style>
